using System;
using System.Collections.Generic;
namespace AppToolKit.Data
{
	public class SqlQuery : TextQuery, IEquatable<SqlQuery>
	{
		public SqlQuery(string sql) : base(sql)
		{
		}
		public SqlQuery(SqlQuery other) : base(other.sb_.ToString())
		{
		}
		public static SqlQuery Format(string format, params object[] args)
		{
			SqlQuery sqlQuery = new SqlQuery();
			sqlQuery.AppendFormat(format, args);
			return sqlQuery;
		}
		public static SqlQuery Format(string format, IDictionary<string, object> dict)
		{
			SqlQuery sqlQuery = new SqlQuery();
			sqlQuery.AppendFormat(format, dict);
			return sqlQuery;
		}
		public SqlQuery()
		{
		}
		public static implicit operator string(SqlQuery right)
		{
			return right.GetValidSql();
		}
		public static implicit operator SqlQuery(string right)
		{
			return new SqlQuery(right);
		}
		public override string ToString()
		{
			return this.GetValidSql();
		}
		public bool Equals(SqlQuery obj)
		{
			return !object.ReferenceEquals(null, obj) && (object.ReferenceEquals(this, obj) || object.Equals(obj.sb_, this.sb_));
		}
		public override bool Equals(object obj)
		{
			return !object.ReferenceEquals(null, obj) && (object.ReferenceEquals(this, obj) || (obj.GetType() == typeof(SqlQuery) && this.Equals((SqlQuery)obj)));
		}
		public override int GetHashCode()
		{
			return (this.sb_ != null) ? this.sb_.GetHashCode() : 0;
		}
		public static bool operator ==(SqlQuery left, SqlQuery right)
		{
			return object.Equals(left, right);
		}
		public static bool operator !=(SqlQuery left, SqlQuery right)
		{
			return !object.Equals(left, right);
		}
		public bool IsValid()
		{
			string text = this.sb_.ToString();
			return SqlInjectHelper.LawlessesCode(text) && SqlInjectHelper.ValidateQuery(text);
		}
		private string GetValidSql()
		{
			return this.sb_.ToString();
		}
		protected string FormatString(object arg)
		{
			string result;
			if (arg is DateTime || (arg is DateTime? && ((DateTime?)arg).HasValue))
			{
				if (arg is DateTime)
				{
					result = ((DateTime)arg).ToString("s");
					return result;
				}
				if (arg is DateTime?)
				{
					result = ((DateTime?)arg).Value.ToString("s");
					return result;
				}
			}
			result = arg.ToString().Trim();
			return result;
		}
		protected override string ProcessArgs(object arg)
		{
			string result;
			if (!(arg is IgnoreCheck))
			{
				if (SqlInjectHelper.LawlessesCode(arg.ToString()) || SqlInjectHelper.ValidateQuery(arg.ToString()))
				{
					throw new SqlQueryException(string.Format("{0}存在非法字符或不允许的命令", arg));
				}
				result = SqlInjectHelper.Escape(this.FormatString(arg));
			}
			else
			{
				result = this.FormatString(arg);
			}
			return result;
		}
	}
}
